# -*- coding: utf-8 -*-
# @Time    : 2020/3/5 下午1:05
# @Author  : lilong


# 单例的装饰器
def singleton(cls, *args, **kwargs):
    """构造一个单例的装饰器"""

    instance = {}
    print("instance:", instance)

    def __singleton(*args, **kwargs):
        if cls not in instance:
            instance[cls] = cls(*args, **kwargs)
        print("instance[cls]:", instance[cls])
        return instance[cls]

    return __singleton


class State():
    """状态的基类"""

    def __init__(self, name):
        self.__name = name

    def getName(self):
        return self.__name


@singleton
class SolidState(State):
    def __init__(self, name):
        super().__init__(name)


# 输出是同一个对象
j = SolidState("li")
print("j:", j)
jj = SolidState("long")
print("jj:", jj)
print(j==jj)

